/*
 * @Author: your name
 * @Date: 2021-04-07 14:40:49
 * @LastEditTime: 2021-04-07 14:59:04
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \git\Javascript-github\YiHzo-Javascript-master\Javascript\数据结构与算法\栈\2. 对象实现.js
 */
class Stack {
  constructor() {
    this.items = [];
    this.count = 0;
  }
  push(element) {
    this.items[this.count] = element;
    this.count++;
  }
  size() {
    return this.count;
  }
  isEmpty() {
    return this.size() === 0;
  }
  pop() {
    if (this.isEmpty()) {
      return undefined;
    }
    this.count--;
    const result = this.items[this.count];
    delete this.items[this.count];
    return result;
  }
  peek() {
    return this.items[this.count - 1];
  }
  clear() {
    this.count = 0;
    this.items = {};
  }
  toString() {
    if (this.isEmpty()) {
      return "";
    }
    let str = this.items["0"];
    for (let i = 0; i < this.count; i++) {
      str = `${str},${this.items[i]}`;
    }
    return str;
  }
}

/**********************************************************/

/**
 * 通用进制转换算法
 * @param {Number} decNumber - 需要转换的数值
 * @param {Number} base - 转换成哪种进制： 2，8，10，16
 * @returns {Number}
 */
function baseConverter(decNumber, base) {
  const stack = new Stack();
  const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let number = decNumber;
  let rem;
  let baseString = "";
  if (!(base >= 2 && base <= 36)) {
    return "";
  }
  while (number > 0) {
    rem = Math.floor(number % base);
    stack.push(rem);
    number = Math.floor(number / base);
  }
  while (!stack.isEmpty()) {
    baseString += digits[stack.pop()];
  }
  return baseString;
}
